import dayjs from 'dayjs'
/**
 *获取当天区间
*/
export const getCurrentToday = (isNeedTime = false) => {
  const startDateTime = isNeedTime ? dayjs().format('YYYY-MM-DD HH:mm:ss') : dayjs().format('YYYY-MM-DD')
  const endDateTime = isNeedTime ? dayjs().format('YYYY-MM-DD HH:mm:ss') : dayjs().format('YYYY-MM-DD')
  // 返回的示例 ["2021-12-13", "2021-12-13"] 或 ["2021-12-13 15:21:58", "2021-12-13 15:21:58"]
  return [startDateTime, endDateTime]
}
/**
 *获取本周
*/
export const getCurrentWeek = (value = null) => {
  // 如果为null,则格式化当前时间
  if (!value) value = new Date()
  const year = value.getFullYear()
  const month = value.getMonth()
  const day = value.getDate()
  const nowDayOfWeek = value.getDay() === 0 ? 7 : value.getDay()
  const week_start_time = new Date(year, month, day - nowDayOfWeek + 1)
  const startDateTime = dayjs(week_start_time).format('YYYY-MM-DD')
  const endDateTime = dayjs(value).format('YYYY-MM-DD')
  return [startDateTime, endDateTime]
}
/**
 *获取本月
*/
export const getCurrentMonth = (value = null) => {
  // 如果为null,则格式化当前时间
  if (!value) value = new Date()
  const year = value.getFullYear()
  const month = value.getMonth()
  const week_start_time = new Date(year, month, 1)
  const startDateTime = dayjs(week_start_time).format('YYYY-MM-DD')
  const endDateTime = dayjs(value).format('YYYY-MM-DD')
  return [startDateTime, endDateTime]
}
/**
 *获取本年
*/
export const getCurrentYear = (value = null) => {
  // 如果为null,则格式化当前时间
  if (!value) value = new Date()
  const year = value.getFullYear()
  const week_start_time = new Date(year, 0, 1)
  const startDateTime = dayjs(week_start_time).format('YYYY-MM-DD')
  const endDateTime = dayjs(value).format('YYYY-MM-DD')
  return [startDateTime, endDateTime]
}
/**
 *选择器区间时间处理
*/
export const getChooseRange = (value = [new Date(), new Date()]) => {
  const startDateTime = dayjs(value[0]).format('YYYY-MM-DD')
  const endDateTime = dayjs(value[1]).format('YYYY-MM-DD')
  return [startDateTime, endDateTime]
}
/**
 *获取当天的年月日
*/
export const getYMD = (value) => {
  if (!value) value = new Date()
  return dayjs(value).format('YYYY-MM-DD')
}
/**
 *时间戳转本地时间
*/
export const getLocalTime = (timeValue) => {
  return new Date(parseInt(timeValue) * 1000)
}
/**
 *获取当天的年月
*/
export const getYM = (value) => {
  return dayjs(value).format('YYYY-MM')
}
/**
 *获取近七天
*/
export const getCurrentDay7 = (value) => {
  if (!value) value = +new Date()
  const one_day = 86400000 // 24 * 60 * 60 * 1000;
  const startDateTime = dayjs(value - 7 * one_day).format('YYYY-MM-DD')
  const endDateTime = dayjs(value).format('YYYY-MM-DD')
  return [startDateTime, endDateTime]
}

/**
 *获取时间戳00:00:00
*/
export const getUnixStart = (startDate) => {
  return dayjs(`${startDate} 00:00:00`).unix()
}

/**
 *获取时间戳23:59:59
*/
export const getUnixEnd = (endDate) => {
  return dayjs(`${endDate} 23:59:59`).unix()
}

export const getCurMonthChinese = (date) => {
  const months = {
    '0': '一月',
    '1': '二月',
    '2': '三月',
    '3': '四月',
    '4': '五月',
    '5': '六月',
    '6': '七月',
    '7': '八月',
    '8': '九月',
    '9': '十月',
    '10': '十一月',
    '11': '十二月'
  }
  return months[ dayjs(date).month()]
}

